// alarm.h 
//	Data structures for a software alarm clock.
//
//	We make use of a hardware timer device, that generates
//	an interrupt every X time ticks (on real systems, X is
//	usually between 0.25 - 10 milliseconds).
//
//	From this, we provide the ability for a thread to be
//	woken up after a delay; we also provide time-slicing.
//
//	NOTE: this abstraction is not completely implemented.
//
// Copyright (c) 1992-1996 The Regents of the University of California.
// All rights reserved.  See copyright.h for copyright notice and limitation 
// of liability and disclaimer of warranty provisions.

#ifndef ALARM_H
#define ALARM_H

#include "copyright.h"
#include "utility.h"
#include "callback.h"
#include "timer.h"
#include "thread.h"
#include "queue"

using namespace std;

typedef struct {
	Thread* th;
	int wake;
} WakeThread;

class WakeComparison
{
public:
	bool operator() (const WakeThread& wt1, const WakeThread& wt2) const
	{
		return (wt1.wake > wt2.wake);
	}
};

typedef priority_queue<WakeThread, vector<WakeThread>, WakeComparison> wakequeue;

// The following class defines a software alarm clock. 
class Alarm : public CallBackObj {
public:
	// Initialize the timer, and callback
	// to "toCall" every time slice.
	Alarm(bool doRandomYield);
	
	~Alarm() { delete timer; }
	
	// suspend execution until time > now + ticksNo
	void WaitUntil(int ticksNo);
//me
        int nr1;
private:
	// the hardware timer device
	Timer *timer;
	
	wakequeue m_qWaitingThreads;
	
	// called when the hardware
	// timer generates an interrupt
	void CallBack();
};

#endif // ALARM_H
